package com.guilin.controller;

import com.guilin.common.core.controller.BaseController;
import com.guilin.common.core.domain.AjaxResult;
import com.guilin.common.core.domain.model.LoginUser;
import com.guilin.common.core.page.TableDataInfo;
import com.guilin.common.utils.SecurityUtils;
import com.guilin.common.utils.poi.ExcelUtil;
import com.guilin.domain.ForumReply;
import com.guilin.system.service.ForumReplyService;
import com.guilin.vo.ForumReplyVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 论坛主题回复Controller
 * 
 * @author xiaoqi
 * @date 2025-06-20
 */
@RestController
@Slf4j
@RequestMapping("/admin-management/forumreply")
public class ForumReplyController extends BaseController
{
    @Autowired
    private ForumReplyService forumReplyService;



    /**
     * 查询论坛主题回复列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumreply:list')")
    @GetMapping("/list")
    public TableDataInfo list(ForumReply forumReply)
    {
        startPage();
        List<ForumReplyVO> list = forumReplyService.selectForumReplyList(forumReply);
        return getDataTable(list);
    }

    /**
     * 导出论坛主题回复列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumreply:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ForumReply forumReply)
    {
        List<ForumReplyVO> list = forumReplyService.selectForumReplyList(forumReply);
        ExcelUtil<ForumReplyVO> util = new ExcelUtil<ForumReplyVO>(ForumReplyVO.class);
        util.exportExcel(response, list, "论坛主题回复数据");
    }

    /**
     * 获取论坛主题回复详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumreply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(forumReplyService.selectForumReplyById(id));
    }

    /**
     * 新增论坛主题回复
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumreply:add')")
    @PostMapping
    public AjaxResult add(@RequestBody ForumReply forumReply)
    {

        return toAjax(forumReplyService.insertForumReply(forumReply));
    }

    /**
     * 修改论坛主题回复
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumreply:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody ForumReply forumReply)
    {
        return toAjax(forumReplyService.updateForumReply(forumReply));
    }

    /**
     * 删除论坛主题回复
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumreply:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Long currentUserId = loginUser.getUser().getUserId();

            // 验证权限
            for (Long id : ids) {
                ForumReply reply = forumReplyService.selectForumReplyById(id);
                if (reply == null || !forumReplyService.isOwner(reply, currentUserId)) {
                    return AjaxResult.error("没有权限删除回复 ID: " + id);
                }
            }

            int result = forumReplyService.deleteForumReplyByIds(ids);

            if (result == 0) {
                return AjaxResult.warn("未删除任何回复，可能已被删除");
            }
            return AjaxResult.success("成功删除 " + result + " 条回复");
        } catch (Exception e) {
            log.error("删除回复失败", e);
            return AjaxResult.error("删除失败: " + e.getMessage());
        }
    }

    /**
     * 获取层级结构的回复列表
     * @param threadId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumreply:list')")
    @GetMapping("/hierarchical/{threadId}")
    public AjaxResult getHierarchicalReplies(@PathVariable Long threadId) {
        List<ForumReplyVO> replies = forumReplyService.getHierarchicalReplies(threadId);
        return success(replies);
    }

    /**
     * 添加子回复
     * @param forumReply
     * @return
     */
    @PreAuthorize("@ss.hasPermi('admin-management:forumreply:add')")
    @PostMapping("/child")
    public AjaxResult addChildReply(@RequestBody ForumReply forumReply) {
        int result = forumReplyService.addChildReply(forumReply);
        return toAjax(result);
    }
}
